const { defineConfig } = require('@vue/cli-service');
const path = require('path');
const webpack = require('webpack');
const CopyWebpackPlugin = require('copy-webpack-plugin');

module.exports = defineConfig({
  transpileDependencies: true,
  devServer: {
    host: 'localhost',
    port: process.env.VUE_APP_PORT || 8080,
    allowedHosts: 'all'
  },
  configureWebpack: {
    plugins: [
      new CopyWebpackPlugin({
        patterns: [
          // Copy Cesium Assets, Widgets, and Workers to a static directory
          {
            from: path.join(__dirname, 'node_modules/cesium/Build/Cesium/Workers'),
            to: 'Workers'
          },
          {
            from: path.join(__dirname, 'node_modules/cesium/Build/Cesium/ThirdParty'),
            to: 'ThirdParty'
          },
          {
            from: path.join(__dirname, 'node_modules/cesium/Build/Cesium/Assets'),
            to: 'Assets'
          },
          {
            from: path.join(__dirname, 'node_modules/cesium/Build/Cesium/Widgets'),
            to: 'Widgets'
          },
          // Copy demo source files to public directory for viewing source code
          {
            from: path.join(__dirname, 'src/demos'),
            to: 'src/demos'
          }
        ]
      }),
      new webpack.DefinePlugin({
        // Define relative base path in cesium for loading assets
        CESIUM_BASE_URL: JSON.stringify('/'),
        __VUE_PROD_HYDRATION_MISMATCH_DETAILS__: JSON.stringify(true)
      })
    ],
    module: {
      unknownContextCritical: false
    },
    optimization: {
      usedExports: true,
      splitChunks: {
        chunks: 'all',
        cacheGroups: {
          cesium: {
            test: /[\\/]node_modules[\\/]cesium/,
            name: 'cesium',
            chunks: 'all',
            priority: 10,
            reuseExistingChunk: true
          }
        }
      }
    }
  },
  chainWebpack: config => {
    config.resolve.alias.set('cesium', path.resolve(__dirname, 'node_modules/cesium'));
    
    // For development, use faster source maps
    if (process.env.NODE_ENV === 'development') {
      config.devtool('eval-source-map');
    }
    
    // 优化图片资源处理
    config.module
      .rule('images')
      .use('url-loader')
      .loader('url-loader')
      .tap(options => Object.assign({}, options, { limit: 10240 })); // 将图片内联限制提高到10KB
    
    // 为Cesium静态资源设置缓存策略
    // 只有当插件存在时才进行配置
    if (config.plugins.has('preload')) {
      config.plugin('preload').tap(options => {
        options[0] = Object.assign({}, options[0], {
          rel: 'preload',
          include: 'initial',
          fileBlacklist: [/\.map$/, /hot-update\.js$/]
        });
        return options;
      });
    }
    
    if (config.plugins.has('prefetch')) {
      config.plugin('prefetch').tap(options => {
        options[0] = Object.assign({}, options[0], {
          fileBlacklist: options[0].fileBlacklist || [/\.map$/, /hot-update\.js$/]
        });
        return options;
      });
    }

  }
});